package com.ft.scan.common;


import org.springframework.context.annotation.Configuration;

import java.util.Random;

/**
 * @author: 幻屹
 * @time: 2024/4/28
 * @destination: 对用户密码进行加密
 */


@Configuration
public class PasswordEncryptionUtils {

    private static final char[] specialCharacters = {'!', '@', '#', '$', '%', '^', '&', '*'};
    private static final char[] mappingTable = {
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
            'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'
    };
    private static final char[] seCretKeyTable = {
            'Z', 'Y', 'X', 'W', 'V', 'U', 'T', 'S', 'R', 'Q', 'P', 'O', 'N', 'M', 'L', 'K', 'J', 'I', 'H', 'G', 'F', 'E', 'D', 'C', 'B', 'A',
            'z', 'y', 'x', 'w', 'v', 'u', 't', 's', 'r', 'q', 'p', 'o', 'n', 'm', 'l', 'k', 'j', 'i', 'h', 'g', 'f', 'e', 'd', 'c', 'b', 'a',
            '9', '8', '7', '6', '5', '4', '3', '2', '1', '0',
    };

    public String encryptPassword(String password) {


        int[][] key = new int[10 + 26 + 26][40];
        StringBuilder tempPassword = new StringBuilder();
//        StringBuilder newPassword = new StringBuilder();
        String newPassword = "";

        if (!password.isEmpty()) {
            for (int i = 0; i < password.length(); i++) {
                password = password.replace(password.charAt(i),
                            password.charAt(i) == '9' ? '0' :
                            password.charAt(i) == 'Z' ? 'A' :
                            password.charAt(i) == 'z' ? 'a' :
                            (char) (password.charAt(i) + 1));
                if (i % 2 == 0) {
                    tempPassword.insert(0, password.charAt(i));
                }else {
                    tempPassword.append(password.charAt(i));
                }
            }

            String temp = tempPassword.toString();

            for (int i = 0; i < temp.length(); i++) {
                for (int j = 0; j < mappingTable.length; j++) {
                    if (temp.charAt(i) == mappingTable[j]) {
                        key[j][i]++;
                    }
                }
            }
            for (int i = 0; i < 10 + 26 + 26; i++) {
                for (int j = 0; j < 40; j++) {
                    if (key[i][j] == 1) {
                        newPassword += seCretKeyTable[i];
                        newPassword += specialCharacters[new Random().nextInt(specialCharacters.length)];
                        newPassword += seCretKeyTable[j];
                        newPassword += specialCharacters[new Random().nextInt(specialCharacters.length)];
                    }
                }
            }
            return newPassword;
        }

        return null;
    }

    public String decryptPassword(String encryptedPassword) {
        if (encryptedPassword == null || encryptedPassword.isEmpty())
            return null;

        String decryptedPassword = "";


        int[][] key = new int[10 + 26 + 26][40];

        for (int i = 0; i < encryptedPassword.length(); i += 4) {
            int iIndex = 0, jIndex = 0;
            for (int j = 0; j < seCretKeyTable.length; j++)  {
                if (encryptedPassword.charAt(i) == seCretKeyTable[j]) {
                    iIndex = j;
                }
                if (encryptedPassword.charAt(i + 2) == seCretKeyTable[j]) {
                    jIndex = j;
                }
            }
            key[iIndex][jIndex]++;
        }

        String onceEncryptPassword = "";

        for (int j = 0; j < 40; j++) {
            for (int i = 0; i < mappingTable.length; i++) {
                if (key[i][j] == 1) {
                    onceEncryptPassword += mappingTable[i];
                }
            }
        }


        if (onceEncryptPassword.length() == 1) {
            return String.valueOf(getTrueLetter(onceEncryptPassword.charAt(0)));
        }

        int left = onceEncryptPassword.length() % 2 == 0 ? onceEncryptPassword.length() / 2 - 1:
                    onceEncryptPassword.length() / 2,
            right = left + 1;

        String originPassword = "";

        while (right < onceEncryptPassword.length() && left >= 0) {
            if (onceEncryptPassword.length() % 2 != 0) {
                originPassword += getTrueLetter(onceEncryptPassword.charAt(left));
                originPassword += getTrueLetter(onceEncryptPassword.charAt(right));
                if (right == onceEncryptPassword.length() - 1) {
                    originPassword += getTrueLetter(onceEncryptPassword.charAt(left - 1));
                    left -= 2;
                }else {
                    left--;
                    right++;
                }
            }else {
                originPassword += getTrueLetter(onceEncryptPassword.charAt(left));
                originPassword += getTrueLetter(onceEncryptPassword.charAt(right));
                left--;
                right++;
            }
        }
        return originPassword;
    }

    private static char getTrueLetter(char letter) {
        return letter == 'A' ? 'Z' :
               letter == '0' ? '9' :
               letter == 'a' ? 'z' :
               (char) (letter - 1);
    }

//    public static void main(String[] args) {
//        PasswordEncryptionUtils passwordEncryptionUtils = new PasswordEncryptionUtils();
//        String password = "1";
//        String encryptedPassword = passwordEncryptionUtils.encryptPassword(password);
//        String decryptedPassword = passwordEncryptionUtils.decryptPassword(encryptedPassword);
//    }

}
